package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.protocol.AbstractProtocolBean;
import com.qdlc.p2p.biz.protocol.BorrowerProtocol;
import com.qdlc.p2p.biz.protocol.ProtocolHelper;
import com.qdlc.p2p.biz.protocol.TenderProtocol;
import com.qdlc.p2p.biz.service.BorrowProtocolService;
import com.qdlc.p2p.common.constant.ProtocolConstant;
import com.qdlc.p2p.dal.dao.BorrowTenderDao;
import com.qdlc.p2p.dal.dao.ProtocolDao;
import com.qdlc.p2p.dal.dto.BorrowTender;
import com.qdlc.p2p.dal.dto.Protocol;

@Service("borrowProtocolService")
public class BorrowProtocolServiceImpl implements BorrowProtocolService {

    @Resource
    private ProtocolDao protocolDao;

    @Resource
    private BorrowTenderDao borrowTenderDao;

    /**
     * 借款人协议下载
     *
     * @param borrowId
     * @param userId
     * @return
     */
    @Override
    public AbstractProtocolBean buildBorrowerProtocol(long borrowId, long userId) {
        Protocol protocol = protocolDao.findByProperty("nid", ProtocolConstant.BASE_FOR_BORROWER).get(0);
        AbstractProtocolBean protocolBean = ProtocolHelper.doProtocol(BorrowerProtocol.class);
        protocolBean.executer(borrowId, protocol, userId);
        return protocolBean;
    }

    /**
     * 投资人协议下载
     *
     * @param tenderId
     * @return
     */
    @Override
    public AbstractProtocolBean buildTenderProtocol(long tenderId) {
        BorrowTender tender = borrowTenderDao.find(tenderId);
        Protocol protocol = protocolDao.findByProperty("nid", ProtocolConstant.BASE_FOR_TENDER).get(0);
        AbstractProtocolBean protocolBean = ProtocolHelper.doProtocol(TenderProtocol.class);
        protocolBean.executer(tender.getBorrow().getId(), tender.getId(), protocol, tender.getUser().getUserId());
        return protocolBean;
    }
}
